package com.zz.tijian.controller;
import com.zz.tijian.entity.Users;
import com.zz.tijian.entity.req.ForgetDto;
import com.zz.tijian.entity.req.LoginDto;
import com.zz.tijian.entity.req.ResetPassDto;
import com.zz.tijian.entity.res.Result;
import com.zz.tijian.entity.res.UserDetailVo;
import com.zz.tijian.expections.CommonException;
import com.zz.tijian.handle.ratelimiter.annotation.DoRateLimiter;
import com.zz.tijian.handle.ratelimiter.res.CustomRateLimiterResponseStrategy;
import com.zz.tijian.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * http://localhost:8080/doc.html
 * http://localhost:8080/swagger-ui/index.html swagger
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/users")
@CrossOrigin
public class UsersController {

    @Autowired
    private UsersService usersService;

    @ApiOperation("测试")
    @GetMapping("/test/{id}")
    @DoRateLimiter(permitsPerSecond = 1, responseStrategy = CustomRateLimiterResponseStrategy.class)
    public Result<List<Users>> test(@PathVariable Integer id){
        if(id < 10){
            throw new CommonException("参数有误");
        }
        List<Users> list = usersService.list();
        return Result.ok(list);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody Users regUser){
        return usersService.register(regUser);
    }

    @ApiOperation("用户登录-手机号和密码")
    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginDto loginDto){
        return usersService.login(loginDto);
    }

    @ApiOperation("用户登录-手机号饥和验证码")
    @PostMapping("/loginByCode")
    public Result<String> loginByCode(@RequestBody LoginDto loginDto){

        return usersService.loginByCode(loginDto);
    }
    @ApiOperation("用户个人信息")
    @GetMapping("/getUserDetail")
    public Result<UserDetailVo> getUserDetail(){
        return usersService.getUserDetail();
    }

    @ApiOperation("获取短信")
    @GetMapping("/getCode/{userId}")
    public Result<Boolean> getCode(@PathVariable String userId){
        return usersService.getCode(userId);
    }

    @ApiOperation("验证短信返回凭证")
    @PostMapping("/forgetPass")
    public Result<String> forgivePass(@RequestBody ForgetDto forgetDto){
        return usersService.forgetPass(forgetDto);
    }

    @ApiOperation("重置密码")
    @PostMapping("/resetPass")
    public Result<Boolean> resetPass(@RequestBody ResetPassDto resetPassDto){
        return usersService.resetPass(resetPassDto);
    }

}
